//
//  LJYRadioViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/28.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYRadioViewController.h"

@interface LJYRadioViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *radioTableView;

@property (nonatomic, retain) NSMutableArray *radioTitleArray;
@property (nonatomic, retain) NSMutableArray *radioArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, retain) NSMutableDictionary *radioDic;

@end

@implementation LJYRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    self.number = -1;
    
    // 创建TableView
    [self createRadioTableView];
}
#pragma mark- 创建TableView
- (void)createRadioTableView
{
    self.radioTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , 375 * SIZE, (667 - 49 - 64) ) style:UITableViewStyleGrouped];
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    
    [self.radioTableView registerClass:[WJYRadioTableViewCell class] forCellReuseIdentifier:@"radioTableViewCell"];
  
    [self.view addSubview:self.radioTableView];
    [self getDataWithURL:@"http://c.3g.163.com/nc/topicset/ios/radio/index.html"];
    
}

- (void)getDataWithURL:(NSString*)url{
    
    self.radioTitleArray = [NSMutableArray array];
    self.radioDic = [NSMutableDictionary dictionary];
    
    [AppTools getDataWithURL:url Block:^(id result) {
        

        // 将数据存入数组
        NSMutableArray *arr = [result objectForKey:@"cList"];
        
        // 遍历数组
        for (NSMutableDictionary *titleDic in arr) {
            
            self.radioArray = [NSMutableArray array];
            // 初始化
            WJYRadioModel *radioTitle = [[WJYRadioModel alloc]init];
            
            // 赋值
            radioTitle.cid = [titleDic objectForKey:@"cid"];
            radioTitle.cname = [titleDic objectForKey:@"cname"];
            
            // 存入数组
            [self.radioTitleArray addObject:radioTitle];
            
            // 取出数据存入数组
            NSMutableArray *arr = [titleDic objectForKey:@"tList"];
            
            // key
            self.number++;
            
            // 遍历数组
            for (NSMutableDictionary *titleDic in arr) {
                
                // 初始化
                WJYRadioModel *radio = [[WJYRadioModel alloc]init];
                
                // 赋值
                radio.tid = [titleDic objectForKey:@"tid"];
                radio.playCount = [titleDic objectForKey:@"playCount"];
                
                // 取出数据存入字典
                NSMutableDictionary *tempDic = [titleDic objectForKey:@"radio"];
                
                
                radio.docid = [tempDic objectForKey:@"docid"];
                radio.title = [tempDic objectForKey:@"tname"];
                radio.subTitle = [tempDic objectForKey:@"title"];
                radio.image = [tempDic objectForKey:@"imgsrc"];
                
                // 添加进数组
                [self.radioArray addObject:radio];
                
                // 装进字典
                [self.radioDic setObject:self.radioArray forKey:[NSString stringWithFormat:@"%ld", self.number]];
                

            }
            
            
            
        }
        
        
        // 重载界面
        [self.radioTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.radioTitleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WJYRadioTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"radioTableViewCell" forIndexPath:indexPath];
    
    NSString *key = [self.radioDic.allKeys objectAtIndex:indexPath.section];
    tableViewCell.radioArray = [self.radioDic objectForKey:key];

    
    return tableViewCell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200 * SIZE;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 40)];
    myView.backgroundColor = [UIColor greenColor];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];;
    nameLabel.text = [self.radioTitleArray[section] cname];
    [myView addSubview:nameLabel];
    
    UILabel *moreLabel = [[UILabel alloc]initWithFrame:CGRectMake(375 - 50, 0, 50, 20)];
    moreLabel.text = @"更多";
    [myView addSubview:moreLabel];
    return myView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
